home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADA / dof_procs.adb < prev    next >
Encoding:
Text File  |  1998-08-12  |  7.2 KB  |  201 lines

  1. --
  2. --  (c) Copyright 1993,1994,1995,1996 Silicon Graphics, Inc.
  3. --  ALL RIGHTS RESERVED
  4. --  Permission to use, copy, modify, and distribute this software for
  5. --  any purpose and without fee is hereby granted, provided that the above
  6. --  copyright notice appear in all copies and that both the copyright notice
  7. --  and this permission notice appear in supporting documentation, and that
  8. --  the name of Silicon Graphics, Inc. not be used in advertising
  9. --  or publicity pertaining to distribution of the software without specific,
  10. --  written prior permission.
  11. --
  12. --  THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. --  AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. --  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. --  FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16. --  GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. --  SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. --  KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. --  LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. --  THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21. --  ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. --  ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. --  POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. --
  25. --  US Government Users Restricted Rights
  26. --  Use, duplication, or disclosure by the Government is subject to
  27. --  restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. --  (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. --  clause at DFARS 252.227-7013 and/or in similar or successor
  30. --  clauses in the FAR or the DOD or NASA FAR Supplement.
  31. --  Unpublished-- rights reserved under the copyright laws of the
  32. --  United States.  Contractor/manufacturer is Silicon Graphics,
  33. --  Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34. --
  35. --  OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. --
  37.  
  38. with GL; use GL;
  39. with Glut; use Glut;
  40. with Jitter;
  41. with Ada.Numerics;
  42. with Ada.Numerics.Generic_Elementary_Functions;
  43.  
  44. package body Dof_Procs is
  45.    package Num renames Ada.Numerics;
  46.    package GLdouble_GEF is new
  47.       Num.Generic_Elementary_Functions (GLdouble);
  48.    use GLdouble_GEF;
  49.  
  50.    procedure accFrustum
  51.       (left  : GLdouble; right : GLdouble; bottom : GLdouble;
  52.        top   : GLdouble; near  : GLdouble; far    : GLdouble;
  53.        pixdx : GLdouble; pixdy : GLdouble; eyedx  : GLdouble;
  54.        eyedy : GLdouble; focus : GLdouble)
  55.    is
  56.       xwsize, ywsize : GLdouble;
  57.       dx, dy : GLdouble;
  58.       viewport : array (0 .. 3) of aliased GLint;
  59.    begin
  60.       glGetIntegerv (GL_VIEWPORT, viewport (0)'Access);
  61.       
  62.       xwsize := right - left;
  63.       ywsize := top - bottom;
  64.       
  65.       dx := -(pixdx * xwsize / GLdouble (viewport (2)) + eyedx * near / focus);
  66.       dy := -(pixdy * ywsize / GLdouble (viewport (3)) + eyedy * near / focus);
  67.  
  68.       glMatrixMode (GL_PROJECTION);
  69.       glLoadIdentity;
  70.       glFrustum (left + dx, right + dx, bottom + dy, top + dy, near, far);
  71.       glMatrixMode (GL_MODELVIEW);
  72.       glLoadIdentity;
  73.       glTranslated (-eyedx, -eyedy, 0.0);
  74.    end accFrustum;
  75.    
  76.    procedure accPerspective
  77.       (fovy  : GLdouble; aspect : GLdouble; near  : GLdouble;
  78.        far   : GLdouble; pixdx  : GLdouble; pixdy : GLdouble;
  79.        eyedx : GLdouble; eyedy  : GLdouble; focus : GLdouble)
  80.    is
  81.       fov2, left, right, bottom, top : GLdouble;
  82.    begin
  83.       fov2 := ((fovy * Num.Pi) / 180.0) / 2.0;
  84.       
  85.       top := near / (Cos (fov2) / Sin (fov2));
  86.       bottom := -top;
  87.       
  88.       right := top * aspect;
  89.       left := -right;
  90.       
  91.       accFrustum (left, right, bottom, top, near, far, pixdx,
  92.          pixdy, eyedx, eyedy, focus);
  93.    end accPerspective;
  94.  
  95.    procedure DoInit is
  96.       ambient : array (0 .. 3) of aliased GLfloat :=
  97.          (0.0, 0.0, 0.0, 1.0);
  98.       diffuse : array (0 .. 3) of aliased GLfloat :=
  99.          (1.0, 1.0, 1.0, 1.0);
  100.       position : array (0 .. 3) of aliased GLfloat :=
  101.          (0.0, 3.0, 3.0, 0.0);
  102.  
  103.       lmodel_ambient : array (0 .. 3) of aliased GLfloat :=
  104.          (0.2, 0.2, 0.2, 1.0);
  105.       local_view : aliased GLfloat := 0.0;
  106.    begin
  107.       glLightfv (GL_LIGHT0, GL_AMBIENT, ambient (0)'Access);
  108.       glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse (0)'Access);
  109.       glLightfv (GL_LIGHT0, GL_POSITION, position (0)'Access);
  110.  
  111.       glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient (0)'Access);
  112.       glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view'Access);
  113.       
  114.       glFrontFace (GL_CW);
  115.       glEnable (GL_LIGHTING);
  116.       glEnable (GL_LIGHT0);
  117.       glEnable (GL_AUTO_NORMAL);
  118.       glEnable (GL_NORMALIZE);
  119.       
  120.       glEnable (GL_DEPTH_TEST);
  121.       glDepthFunc (GL_LESS);
  122.       
  123.       glMatrixMode (GL_MODELVIEW);
  124.       glLoadIdentity;
  125.       
  126.       glClearColor (0.0, 0.0, 0.0, 0.0);
  127.       glClearAccum (0.0, 0.0, 0.0, 0.0);
  128.    end DoInit;
  129.  
  130.    procedure renderTeapot
  131.       (x     : GLfloat; y     : GLfloat; z     : GLfloat;
  132.        ambr  : GLfloat; ambg  : GLfloat; ambb  : GLfloat;
  133.        difr  : GLfloat; difg  : GLfloat; difb  : GLfloat;
  134.        specr : GLfloat; specg : GLfloat; specb : GLfloat;
  135.        shine : GLfloat)
  136.    is
  137.       mat : array (0 .. 3) of aliased GLfloat;
  138.    begin
  139.       glPushMatrix;
  140.  
  141.       glTranslatef (x, y, z);
  142.  
  143.       mat (0) := ambr;
  144.       mat (1) := ambg;
  145.       mat (2) := ambb;
  146.       mat (3) := 1.0;
  147.       glMaterialfv (GL_FRONT, GL_AMBIENT, mat (0)'Access);
  148.  
  149.       mat (0) := difr;
  150.       mat (1) := difg;
  151.       mat (2) := difb;
  152.       glMaterialfv (GL_FRONT, GL_DIFFUSE, mat (0)'Access);
  153.  
  154.       mat (0) := specr;
  155.       mat (1) := specg;
  156.       mat (2) := specb;
  157.       glMaterialfv (GL_FRONT, GL_SPECULAR, mat (0)'Access);
  158.  
  159.       glMaterialf (GL_FRONT, GL_SHININESS, shine * 128.0);
  160.  
  161.       glutSolidTeapot (0.5);
  162.       glPopMatrix;
  163.    end renderTeapot;
  164.  
  165.    procedure DoDisplay is
  166.       viewport : array (0 .. 3) of aliased GLint;
  167.    begin
  168.       glGetIntegerv (GL_VIEWPORT, viewport (0)'Access);
  169.       glClear (GL_ACCUM_BUFFER_BIT);
  170.       
  171.       for jitval in 1 .. 8 loop
  172.          glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  173.             accPerspective (45.0, GLdouble (viewport (2)) /
  174.             GLdouble (viewport (3)),
  175.             1.0, 15.0, 0.0, 0.0, GLdouble (0.33 * Jitter.j8 (jitval).x),
  176.             GLdouble (0.33 * Jitter.j8 (jitval).y), 5.0);
  177.  
  178.          renderTeapot (-1.1, -0.5, -4.5, 0.1745, 0.01175, 0.01175,
  179.             0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
  180.          renderTeapot (-0.5, -0.5, -5.0, 0.24725, 0.1995, 0.0745,
  181.             0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
  182.          renderTeapot (0.2, -0.5, -5.5, 0.19225, 0.19225, 0.19225,
  183.             0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
  184.          renderTeapot (1.0, -0.5, -6.0, 0.0215, 0.1745, 0.0215,
  185.             0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
  186.          renderTeapot (1.8, -0.5, -6.5, 0.0, 0.1, 0.06, 0.0, 0.50980392,
  187.             0.50980392, 0.50196078, 0.50196078, 0.50196078, 0.25);
  188.          glAccum (GL_ACCUM, 0.125);
  189.       end loop;
  190.       
  191.       glAccum (GL_RETURN, 1.0);
  192.       glFlush;
  193.    end DoDisplay;
  194.  
  195.  
  196.    procedure ReshapeCallback (w : Integer; h : Integer) is
  197.    begin
  198.       glViewport (0, 0, GLsizei(w), GLsizei(h));
  199.    end ReshapeCallback;
  200. end Dof_Procs;
  201.